from rest_framework import serializers
from . import models


class CourseCategorySerializer(serializers.ModelSerializer):
    """课程分类序列化类"""

    class Meta:
        model = models.CourseCategory
        fields = ['id', 'name']


class TeacherSerializer(serializers.ModelSerializer):
    """老师序列化类"""

    class Meta:
        model = models.Teacher
        fields = ['name', 'title', 'role_name', 'signature', 'image', 'brief']


class CourseModelSerializer(serializers.ModelSerializer):
    """课程序列化类"""
    # 子序列化的方式引用老师表的字段
    teacher = TeacherSerializer()

    class Meta:
        model = models.Course
        fields = ['id',
                  'name',
                  'course_img',
                  'brief',
                  'attachment_path',
                  'pub_sections',
                  'price',
                  'students',
                  'period',
                  'sections',
                  'course_type_name',
                  'level_name',
                  'status_name',
                  'teacher',
                  'section_list', ]


class CourseSectionSerializer(serializers.ModelSerializer):
    """课时序列化类"""

    class Meta:
        model = models.CourseSection
        fields = ['name', 'orders', 'duration', 'free_trail', 'section_link', 'section_type_name']


class CourseChapterSerializer(serializers.ModelSerializer):
    """章节序列化类"""
    # 子序列化
    coursesections = CourseSectionSerializer(many=True)  # 一个章节下有很多课时，所以要有参数many=True

    class Meta:
        model = models.CourseChapter
        fields = ['name', 'summary', 'chapter', 'coursesections']
